package com.hfzy.ihk.web.freeswitchManage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.CommonUtils;
import com.hfzy.ihk.facade.freeswitchManage.enums.CardStatusEnum;
import com.hfzy.ihk.facade.freeswitchManage.enums.EquipmentEnum;
import com.hfzy.ihk.web.freeswitchManage.biz.EquipmentInfoBiz;
import com.hfzy.ihk.web.freeswitchManage.biz.PushToWebsocketBiz;
import com.hfzy.ihk.web.freeswitchManage.mapper.GatewayCardSlotMapper;
import com.hfzy.ihk.web.freeswitchManage.vo.EquimentInfo;
import com.hfzy.ihk.web.freeswitchManage.vo.EquimentSlotInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by YANFA on 2019/3/26.
 */
@RestController
@RequestMapping("gatewayOpt")
@Slf4j
public class GatewayOptController {

    @Autowired
    PushToWebsocketBiz pushToWebsocketBiz;

    @Autowired
    private EquipmentInfoBiz equipmentInfoBiz;

    @Autowired
    private GatewayCardSlotMapper gatewayCardSlotMapper;

    @RequestMapping("receive")
    public String receive(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String bodyStr = CommonUtils.getInputStr(httpServletRequest);
        try {
            if (bodyStr == null)
                return "0";


            JSONObject data = JSON.parseObject(bodyStr);
            if (!data.containsKey("sn"))
                return "0";

            String sn = data.getString("sn");

            if (data.containsKey("device")) {//注册消息
                log.info("==========>获取鼎迅通设备注册消息 数据：{}",bodyStr);
                JSONArray deviceArray = data.getJSONArray("device");

                if (deviceArray.size() == 0)
                    return "0";

                JSONObject deviceJson = deviceArray.getJSONObject(0);
                String port_number = deviceJson.getString("port_number");

                EquimentInfo equimentInfo = new EquimentInfo();

                equimentInfo.setSn(sn);
                equimentInfo.setChannelNumber(Integer.valueOf(port_number));
                equimentInfo.setEquipmentDealer(EquipmentEnum.DINSTAR);
               // equipmentInfoBiz.createEquipment(equimentInfo);

            } else if (data.containsKey("register")) {//卡注册消息
                log.info("==========>获取鼎迅通卡注册消息 数据：{}",bodyStr);
                JSONArray registerArray = data.getJSONArray("register");
                if (registerArray.size() == 0)
                    return "0";
                JSONObject registerJson = registerArray.getJSONObject(0);
                String port = registerJson.getString("port");
                String iccid = registerJson.getString("iccid");
                String imsi = registerJson.getString("imsi");
                String status = registerJson.getString("status");
                if ("up".equals(status)) {
                    EquimentSlotInfo equimentSlotInfo = new EquimentSlotInfo();
                    equimentSlotInfo.setStatus(CardStatusEnum.UP);
                    equimentSlotInfo.setSn(sn);
                    equimentSlotInfo.setChannel(port);
                    equimentSlotInfo.setSlotId(port);
                    equimentSlotInfo.setCcid(iccid);
                    //equipmentInfoBiz.createSolt(equimentSlotInfo);

                } else if ("down".equals(status)) {
                    EquimentSlotInfo equimentSlotInfo = new EquimentSlotInfo();
                    equimentSlotInfo.setStatus(CardStatusEnum.DOWN);
                    equimentSlotInfo.setSn(sn);
                    equimentSlotInfo.setChannel(port);
                    equimentSlotInfo.setSlotId(port);
                    equimentSlotInfo.setCcid(iccid);
                    //equipmentInfoBiz.createSolt(equimentSlotInfo);
                } else {
                    log.info(">>>>>>>>>>.未识别到的鼎迅通识别消息：{}", bodyStr);
                }

            } else if (data.containsKey("cdr")) {//账单消息

            } else {
                log.info("鼎迅通设备未识别消息：{}", bodyStr);
            }
        }catch (Exception e){
            log.error(">>>>>>>>处理鼎迅通设备消息异常:{}",bodyStr,e);
        }

        return "1";

    }

    /*
    测试用
     */
    @RequestMapping("changeSwxActiveCard")
    public String changeSwxActiveCard(String sn,String channel,String slotIndex){
        pushToWebsocketBiz.changeActiveCard(sn,channel,slotIndex);
        return "成功";
    }

    /**
     * 测试用
     * @param serverId
     * @param data
     * @return
     */

    @RequestMapping("pushActionByServerId")
    public String pushActionByServerId(String serverId,String data){
        pushToWebsocketBiz.checkChannelTrunk(serverId);
        return "成功";
    }

    /**
     * 自己调用用，用于如果ccid的数据后面过来，可以重新将通道的city加载进redis
     * @return
     */
    @RequestMapping("loadAllChannelCityToRedis")
    public String loadAllChannelCityToRedis(){

       return "";
    }


}
